Page({
  data: {
    countdown: 3,
    timer: null
  },

  onLoad() {
    this.startCountdown();
  },

  startCountdown() {
    this.setData({
      countdown: 3
    });
    
    this.data.timer = setInterval(() => {
      let count = this.data.countdown - 1;
      
      if (count <= 0) {
        this.skipToHome();
      } else {
        this.setData({
          countdown: count
        });
      }
    }, 1000);
  },

  skipToHome() {
    clearInterval(this.data.timer);
    wx.switchTab({
      url: '/pages/index/index'
    });
  },

  onUnload() {
    if (this.data.timer) {
      clearInterval(this.data.timer);
    }
  }
});
